<template>
  <div class="worktask-designer-container">
    <!-- <top-tip :tipcontent="tipcontent" /> -->
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space wrap :size="size">
            <!-- <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.city"
                :options="cityOptions"
                :props="citySelect"
                placeholder="按地区"
                clearable
              />
            </el-form-item>
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.industry"
                :options="industryOptions"
                :props="industrySelect"
                placeholder="按行业"
                clearable
              />
            </el-form-item> -->
            <el-form-item>
              <el-input
                v-model.trim="queryForm.searchKey"
                clearable
                placeholder="搜客户名称"
              />
            </el-form-item>
            <!-- <el-form-item>
              <el-input
                v-model.trim="queryForm.pickPeople"
                clearable
                placeholder="搜店铺账号"
              />
            </el-form-item> -->
            <!-- <el-form-item>
              <el-date-picker
                v-model="value1"
                type="date"
                placeholder="请选择派单时间"
              />
              <DateSelect ref="DateSelectRef" @get-date="getDate" />
            </el-form-item> -->
            <el-form-item>
              <el-input
                v-model.trim="queryForm.OrderCode"
                clearable
                placeholder="按合同编号"
              />
            </el-form-item>
            <el-form-item
              v-if="
                queryForm.tab == 'serviceing' ||
                queryForm.tab == 'allServiceing'
              "
            >
              <el-select
                v-model="queryForm.searchDateType"
                clearable
                placeholder="日期类型"
                style="width: 150px"
              >
                <el-option label="按服务起始日" :value="1" />
                <el-option label="按服务到期日" :value="-1" />
              </el-select>
            </el-form-item>
            <el-form-item
              v-if="
                queryForm.tab == 'serviceing' ||
                queryForm.tab == 'allServiceing'
              "
            >
              <el-date-picker
                v-model="queryForm.serviceDates"
                style="width: 240px"
                type="daterange"
                placeholder="请选择日期区间"
                start-placeholder="日期起"
                end-placeholder="日期止"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <el-form-item
              v-if="
                queryForm.tab == 'serviceing' ||
                queryForm.tab == 'allServiceing'
              "
            >
              <el-select
                v-model="queryForm.isSerivceRecord"
                clearable
                placeholder="有无小记"
                style="width: 100px"
              >
                <el-option label="有小记" :value="1" />
                <el-option label="无小记" :value="-1" />
              </el-select>
            </el-form-item>
            <el-form-item
              v-if="
                queryForm.tab == 'serviceing' ||
                queryForm.tab == 'allServiceing'
              "
            >
              <el-date-picker
                v-model="queryForm.TrackRecordDates"
                style="width: 280px"
                type="daterange"
                placeholder="按服务小记查询"
                start-placeholder="服务小记日期起"
                end-placeholder="服务小记日期止"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane label="新分待运营任务" name="new">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          /> -->
          <el-table-column
            align="center"
            prop="WorkTaskID"
            label="任务ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="BusinessOrderID"
            label="合同ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="OrderCode"
            label="合同编号"
            width="120"
          />
          <el-table-column
            align="center"
            prop="OrderType"
            label="合同属性"
            width="80"
          />
          <el-table-column
            align="center"
            prop="AddTime"
            label="派单时间"
            width="200"
          />
          <el-table-column
            align="center"
            prop="AssignStaffName"
            label="派单人"
            width="100"
          />
          <el-table-column
            align="left"
            prop="CustomerAllName"
            label="客户名称"
          />
          <el-table-column
            align="center"
            prop="ProductName"
            label="业务服务产品"
            width="200"
          />
          <el-table-column
            align="center"
            prop="BindStoreAccount"
            label="运营店铺/账号"
          />
          <el-table-column
            align="center"
            prop="TaskStaffName"
            label="接单人"
            width="100"
          />
          <el-table-column align="center" label="分单状态" width="150">
            <span style="color: #1890ff">新分单待接单</span>
          </el-table-column>
          <el-table-column
            align="center"
            label="操作"
            fixed="right"
            width="100"
          >
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="receiveTask(row)">
                接单
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="接单待服务" name="await">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          /> -->
          <el-table-column
            align="center"
            prop="WorkTaskID"
            label="任务ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="BusinessOrderID"
            label="合同ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="OrderCode"
            label="合同编号"
            width="120"
          />
          <el-table-column
            align="center"
            prop="OrderType"
            label="合同属性"
            width="80"
          />
          <el-table-column
            align="center"
            prop="AddTime"
            label="派单时间"
            width="200"
          />
          <el-table-column
            align="center"
            prop="AssignStaffName"
            label="派单人"
            width="100"
          />
          <el-table-column
            align="left"
            prop="CustomerAllName"
            label="客户名称"
          />
          <el-table-column
            align="center"
            prop="ProductName"
            label="业务服务产品"
            width="150"
          />
          <el-table-column
            align="center"
            prop="BindStoreAccount"
            label="运营店铺/账号"
          />
          <el-table-column align="center" label="实际服务期限" width="150">
            <template #default="{ row }">
              {{ row.ServiceStartDate }}至{{ row.ServiceEndDate }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="TaskStaffName"
            label="接单人"
            width="100"
          />
          <el-table-column
            align="center"
            label="查看任务并处理"
            fixed="right"
            width="150"
          >
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="运营服务中" name="serviceing">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          /> -->
          <el-table-column
            align="center"
            prop="WorkTaskID"
            label="任务ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="BusinessOrderID"
            label="合同ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="OrderCode"
            label="合同编号"
            width="120"
          />
          <el-table-column
            align="center"
            prop="OrderType"
            label="合同属性"
            width="80"
          />
          <el-table-column
            align="center"
            prop="AddTime"
            label="派单时间"
            width="200"
          />
          <el-table-column
            align="center"
            prop="AssignStaffName"
            label="派单人"
            width="100"
          />
          <el-table-column
            align="left"
            prop="CustomerAllName"
            label="客户名称"
          />
          <el-table-column
            align="center"
            prop="ProductName"
            label="业务服务产品"
            width="200"
          />
          <el-table-column
            align="center"
            prop="BindStoreAccount"
            label="运营店铺/账号"
          />
          <el-table-column align="center" label="实际服务期限" width="150">
            <template #default="{ row }">
              {{ row.ServiceStartDate }}至{{ row.ServiceEndDate }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="TaskStaffName"
            label="接单人"
            width="100"
          />
          <!-- <el-table-column align="center" label="分单状态" width="150">
            <span style="color: #67c23a">已接单服务中</span>
          </el-table-column> -->
          <el-table-column
            align="center"
            label="查看任务并处理"
            fixed="right"
            width="150"
          >
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="运营暂停中" name="paused">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          /> -->
          <el-table-column
            align="center"
            prop="WorkTaskID"
            label="任务ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="BusinessOrderID"
            label="合同ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="OrderCode"
            label="合同编号"
            width="120"
          />
          <el-table-column
            align="center"
            prop="OrderType"
            label="合同属性"
            width="80"
          />
          <el-table-column
            align="center"
            prop="AddTime"
            label="派单时间"
            width="200"
          />

          <el-table-column
            align="center"
            prop="AssignStaffName"
            label="派单人"
            width="100"
          />
          <el-table-column
            align="left"
            prop="CustomerAllName"
            label="客户名称"
          />
          <el-table-column
            align="center"
            prop="ProductName"
            label="业务服务产品"
            width="200"
          />
          <el-table-column
            align="center"
            prop="BindStoreAccount"
            label="运营店铺/账号"
          />
          <el-table-column align="center" label="实际服务期限" width="150">
            <template #default="{ row }">
              {{ row.ServiceStartDate }}至{{ row.ServiceEndDate }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="TaskStaffName"
            label="接单人"
            width="100"
          />
          <!-- <el-table-column align="center" label="分单状态" width="150">
            <span style="color: #fa8c16">暂停服务中</span>
          </el-table-column> -->
          <el-table-column
            align="center"
            label="查看任务并处理"
            fixed="right"
            width="150"
          >
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="已终止服务" name="terminated">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          /> -->
          <el-table-column
            align="center"
            prop="WorkTaskID"
            label="任务ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="BusinessOrderID"
            label="合同ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="OrderCode"
            label="合同编号"
            width="120"
          />
          <el-table-column
            align="center"
            prop="OrderType"
            label="合同属性"
            width="80"
          />
          <el-table-column
            align="center"
            prop="AddTime"
            label="派单时间"
            width="200"
          />

          <el-table-column
            align="center"
            prop="AssignStaffName"
            label="派单人"
            width="100"
          />
          <el-table-column
            align="left"
            prop="CustomerAllName"
            label="客户名称"
          />
          <el-table-column
            align="center"
            prop="ProductName"
            label="业务服务产品"
            width="150"
          />
          <el-table-column
            align="center"
            prop="BindStoreAccount"
            label="运营店铺/账号"
          />
          <el-table-column
            align="center"
            prop="TaskStaffName"
            label="接单人"
            width="100"
          />
          <el-table-column align="center" label="实际服务期限" width="150">
            <template #default="{ row }">
              {{ row.ServiceStartDate }}至{{ row.ServiceEndDate }}
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" label="分单状态" width="150">
            <span style="color: #ff0000">已终止服务</span>
          </el-table-column> -->
          <el-table-column
            align="center"
            label="查看任务并处理"
            fixed="right"
            width="150"
          >
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="所属运营服务中" name="allServiceing">
        <el-table v-loading="listLoading" :data="list">
          <el-table-column
            align="center"
            prop="WorkTaskID"
            label="任务ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="BusinessOrderID"
            label="合同ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="OrderCode"
            label="合同编号"
            width="120"
          />
          <el-table-column
            align="center"
            prop="OrderType"
            label="合同属性"
            width="80"
          />
          <el-table-column
            align="center"
            prop="AddTime"
            label="派单时间"
            width="200"
          />

          <el-table-column
            align="center"
            prop="AssignStaffName"
            label="派单人"
            width="100"
          />
          <el-table-column
            align="left"
            prop="CustomerAllName"
            label="客户名称"
          />
          <el-table-column
            align="center"
            prop="ProductName"
            label="业务服务产品"
            width="150"
          />
          <el-table-column
            align="center"
            prop="BindStoreAccount"
            label="运营店铺/账号"
          />
          <el-table-column
            align="center"
            prop="TaskStaffName"
            label="接单人"
            width="100"
          />
          <el-table-column align="center" label="实际服务期限" width="200">
            <template #default="{ row }">
              {{ row.ServiceStartDate }}至{{ row.ServiceEndDate }}
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" label="分单状态" width="150">
            <template #default="{ row }">
              <span v-if="row.ServiceState == 2" style="color: #1890ff">
                新分单待接单
              </span>
              <span v-if="row.ServiceState == 3" style="color: #67c23a">
                已接单服务中
              </span>
              <span v-if="row.ServiceState == 4" style="color: #fa8c16">
                暂停服务中
              </span>
              <span v-if="row.ServiceState == 5" style="color: #ff0000">
                已终止服务
              </span>
            </template>
          </el-table-column> -->
          <el-table-column
            align="center"
            fixed="right"
            label="查看任务并处理"
            width="160"
          >
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="getTackinfo(row)">
                查看任务并处理
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
    <edit ref="editRef" @fetch-data="fetchData" />

    <!-- <el-dialog
      v-model="dialogFormVisible"
      title="接单"
      width="500px"
      @close="dialogClose"
    >
      <el-form-item label="设定服务期限">
        <el-date-picker
          v-model="queryForm.SeviceDates"
          type="daterange"
          start-placeholder="服务开始日期"
          end-placeholder="服务结束日期"
        />
      </el-form-item>
      <template #footer>
        <el-button @click="dialogClose">取 消</el-button>
        <el-button type="primary" @click="receiveTask(queryForm)">
          确 定
        </el-button>
      </template>
    </el-dialog> -->
  </div>
</template>
<script>
  // import DateSelect from '/src/components/DateSelect.vue'
  import { CitySelect, IndustrySelect } from '@/api/common'
  import { getList, doDelete, OperateGetOrder } from '@/api/storeOperate'
  import Edit from './components/StoreEdit'
  // import TopTip from '@/components/TopTip'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'AllStore',
    components: { Edit },
    setup() {
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        tipcontent: {
          title: '运营任务',
          memo:
            '本页用于“运营师”接收所被分配的工作任务；默认显示尚未接受的运营任务，收到后先进行接受，进入到处理中，并相应进行任务的跟进记录；' +
            '处理完成后，提交相关信息，等待管理员审核。',
        },
        editRef: null,
        list: [],
        clickList: {},
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        DateSelectRef: null,
        cityOptions: [],
        industryOptions: [],
        citySelect: { checkStrictly: true, multiple: true },
        industrySelect: { checkStrictly: true, multiple: true },

        dialogFormVisible: false,
        selectRows: '',
        queryForm: {
          dataType: 'storeManager',
          tab: 'new',
          AssignOrderSetID: 2,
          TaskState: 0,
          pageNo: 1,
          pageSize: 10,
          searchKey: '',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const getTackinfo = (row) => {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        const actstaffpost = JSON.parse(userInfo.StaffPostID)
        const actstaffpostid = actstaffpost[0][1]
        router.push({
          path: 'storeDetails',
          query: {
            actstaffpostid: actstaffpostid,
            OrderID: row.BusinessOrderID,
            WorkTaskID: row.WorkTaskID,
          },
        })
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }

      const toRecord = (row) => {
        state.clickList = row
        state.dialogFormVisible = true
      }
      // 领取任务
      const receiveTask = async (row) => {
        console.log(row, '1111')
        // if (!row.SeviceDates) {
        //   $baseMessage('请选择服务期限', 'error', 'vab-hey-message-error')
        // } else {
        //   const dataMsg = {
        //     WorkTaskID: state.clickList.WorkTaskID,
        //     SeviceDates: row.SeviceDates,
        //   }
        //   const { msg } = await OperateGetOrder(dataMsg)
        //   $baseMessage(msg, 'success', 'vab-hey-message-success')
        //   state.dialogFormVisible = false
        //   state.clickList = {}
        //   fetchData()
        // }
        const dataMsg = {
          WorkTaskID: row.WorkTaskID,
        }
        const { msg } = await OperateGetOrder(dataMsg)
        $baseMessage(msg, 'success', 'vab-hey-message-success')
        state.dialogFormVisible = false
        state.clickList = {}
        state.queryForm.tab = 'await'
        fetchData()
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        state.listLoading = true
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        const actstaffpost = JSON.parse(userInfo.StaffPostID)
        state.ActStaffPostID = actstaffpost[0][1]

        if (state.ActStaffPostID == 20) state.queryForm.AssignOrderSetID = 2
        if (state.ActStaffPostID == 20) state.queryForm.AssignOrderSetID = 2

        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }

      const dialogClose = () => {
        state.dialogFormVisible = false
      }
      const handleTabClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }
      onMounted(() => {
        fetchData()
        getCitySelect()
        getIndustrySelect()
      })

      const getCitySelect = async () => {
        const {
          data: { cityOptions },
        } = await CitySelect()
        state.cityOptions = cityOptions
        console.log('列表1', cityOptions)
      }
      const getIndustrySelect = async () => {
        const {
          data: { industryOptions },
        } = await IndustrySelect()
        state.industryOptions = industryOptions
        console.log('列表2', industryOptions)
      }

      const getDate = (data) => {
        state.queryForm.orderDate = data
        console.log(data, '子组件传过来的时间数组')
      }

      return {
        ...toRefs(state),
        setSelectRows,
        dialogClose,
        getTackinfo,
        handleTabClick,
        receiveTask,
        toRecord,
        handleEdit,
        getDate,
        getCitySelect,
        getIndustrySelect,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
